More QString love sprinkled around. Built better infrastructure in csv_utils;
authorrobertlipe@gmail.com <robertlipe@gmail.com@f51c46e8-681c-474f-0cfe-069cfd0219fb>
Tue, 31 Dec 2013 23:59:16 +0000 (23:59 +0000)
committerrobertlipe@gmail.com <robertlipe@gmail.com@f51c46e8-681c-474f-0cfe-069cfd0219fb>
Tue, 31 Dec 2013 23:59:16 +0000 (23:59 +0000)
ozi and tmpro now more consistently use QStrings internally.

git-svn-id: http://gpsbabel.googlecode.com/svn/trunk@4687 f51c46e8-681c-474f-0cfe-069cfd0219fb

gpsbabel/csv_util.cc
gpsbabel/csv_util.h
gpsbabel/ozi.cc
gpsbabel/tmpro.cc

index a766aa5ae37b0d47501f0dd50f7cb2ec55f2befa..52a47088517410d667559b2acccbe18657a9d530 100644 (file)
@@ -23,6 +23,7 @@
 #include <ctype.h>
 #include <math.h>
 #include <stdlib.h>
+#include <QtCore/QRegExp>
 #include "defs.h"
 #include "csv_util.h"
 #include "grtcirc.h"
@@ -174,6 +175,7 @@ static UrlLink* link_;
 /*     usage: p = csv_stringclean(stringtoclean, "&,\"")             */
 /*            (strip out ampersands, commas, and quotes.             */
 /*********************************************************************/
+
 char*
 #ifdef DEBUG_MEM
 CSV_STRINGCLEAN(const char* string, const char* chararray, DEBUG_PARAMS)
@@ -213,14 +215,14 @@ csv_stringclean(const char* string, const char* chararray)
 }
 
 QString
-csv_stringclean(const QString& string, const char* chararray)
+csv_stringclean(const QString& source, const QString& to_nuke)
 {
-  char *t = csv_stringclean(CSTR(string), chararray);
-  QString r(t);
-  xfree(t);
-  return r;
+  QString r = source;
+  QString regex = QString("[%1]").arg(to_nuke);
+  return r.remove(QRegExp(regex));
 }
 
+#if 0
 char*
 csv_stringclean(const QString& string_in, const QString& chararray_in)
 {
@@ -235,6 +237,7 @@ csv_stringclean(const QString& string_in, const QString& chararray_in)
 //  xfree(chararray);
   return r;
 }
+#endif
 
 /***********************************************************************************/
 /* csv_stringtrim() - trim whitespace and leading and trailing enclosures (quotes) */
@@ -301,6 +304,15 @@ csv_stringtrim(const char* string, const char* enclosure, int strip_max)
   return (tmp);
 }
 
+// Is this really the replacement for the above?  
+QString
+csv_stringtrim(const QString& source, const QString& enclosure) 
+{
+  QString r = source;
+  r.replace(enclosure, "");
+  return r.trimmed();
+}
+
 /*****************************************************************************/
 /* csv_lineparse() - extract data fields from a delimited string. designed   */
 /*                   to handle quoted and delimited data within quotes.      */
index 356848135f6c96b424da498abfd18f1b8303871e..87c98a61ba8b635f97e698f0f64e6b74f68dadb5 100644 (file)
@@ -26,6 +26,7 @@ csv_stringtrim(const char* string, const char* enclosure, int strip_max);
 CSV_STRINGTRIM(const char* string, const char* enclosure, int strip_max, DEBUG_PARAMS);
 #define csv_stringtrim( s, e,m ) CSV_STRINGTRIM( s, e, m, __FILE__, __LINE__)
 #endif
+QString csv_stringtrim(const QString& source, const QString& enclosure);
 
 char*
 csv_lineparse(const char* stringstart, const char* delimited_by, const char* enclosed_in, const int line_no);
@@ -40,7 +41,7 @@ csv_stringclean(const char* string, const char* chararray);
 CSV_STRINGCLEAN(const char* string, const char* chararray,DEBUG_PARAMS);
 #define csv_stringclean(s,c) CSV_STRINGCLEAN(s,c,__FILE__,__LINE__)
 #endif
-QString csv_stringclean(const QString& string, const char* chararray);
+QString csv_stringclean(const QString& string, const QString& chararray);
 
 void
 xcsv_data_read(void);
index c10210c073175722d7cd56a6e17d0c924fcdaba1..c9a32c20c7414ab1c9fb1e314003ed8d78d2dd44 100644 (file)
@@ -159,9 +159,9 @@ ozi_get_time_str(const waypoint* waypointp, char* buff, gbsize_t buffsz)
 }
 
 void
-ozi_set_time_str(const char* str, waypoint* waypointp)
+ozi_set_time_str(const QString& str, waypoint* waypointp)
 {
-  double ozi_time = atof(str);
+  double ozi_time = str.toDouble();
 
   if (ozi_time > DAYS_SINCE_1990) {
     waypointp->SetCreationTime((ozi_time - DAYS_SINCE_1990) * SECONDS_PER_DAY,
@@ -504,11 +504,11 @@ wr_deinit(void)
 }
 
 static void
-ozi_parse_waypt(int field, char* str, waypoint* wpt_tmp, ozi_fsdata* fsdata)
+ozi_parse_waypt(int field, const QString& str, waypoint* wpt_tmp, ozi_fsdata* fsdata)
 {
   double alt;
 
-  if (*str == '\0') {
+  if (str.isEmpty()) {
     return;
   }
 
@@ -518,15 +518,15 @@ ozi_parse_waypt(int field, char* str, waypoint* wpt_tmp, ozi_fsdata* fsdata)
     break;
   case 1:
     /* waypoint name */
-    wpt_tmp->shortname = csv_stringtrim(str, "", 0);
+    wpt_tmp->shortname = csv_stringtrim(str, "");
     break;
   case 2:
     /* degrees latitude */
-    wpt_tmp->latitude = atof(str);
+    wpt_tmp->latitude = str.toDouble();
     break;
   case 3:
     /* degrees longitude */
-    wpt_tmp->longitude = atof(str);
+    wpt_tmp->longitude = str.toDouble();
     break;
   case 4:
     /* DAYS since 1900 00:00:00 in days.days (5.5) */
@@ -538,7 +538,7 @@ ozi_parse_waypt(int field, char* str, waypoint* wpt_tmp, ozi_fsdata* fsdata)
        tables are, so we read just the numbers.  This converts badly to
        other types, but it at least maintains fidelity for an ozi->ozi
        operation. */
-    if (str && isdigit(str[0])) {
+    if (str.toInt() > 0) {
       wpt_tmp->icon_descr = str;
     }
     break;
@@ -550,15 +550,15 @@ ozi_parse_waypt(int field, char* str, waypoint* wpt_tmp, ozi_fsdata* fsdata)
     break;
   case 8:
     /* foreground color (0=black) */
-    fsdata->fgcolor = atoi(str);
+    fsdata->fgcolor = str.toInt();
     break;
   case 9:
     /* background color (65535=yellow) */
-    fsdata->bgcolor = atoi(str);
+    fsdata->bgcolor = str.toInt();
     break;
   case 10:
     /* Description */
-    wpt_tmp->description = csv_stringtrim(str, "", 0);
+    wpt_tmp->description = csv_stringtrim(str, "");
     break;
   case 11:
     /* pointer direction 0,1,2,3 bottom,top,left,right */
@@ -568,11 +568,11 @@ ozi_parse_waypt(int field, char* str, waypoint* wpt_tmp, ozi_fsdata* fsdata)
     break;
   case 13:
     /* proximity distance - meters */
-    WAYPT_SET(wpt_tmp, proximity, atof(str) * prox_scale);
+    WAYPT_SET(wpt_tmp, proximity, str.toDouble() * prox_scale);
     break;
   case 14:
     /* altitude */
-    alt = atof(str);
+    alt = str.toDouble();
     if (alt == -777) {
       wpt_tmp->altitude = unknown_alt;
     } else {
@@ -666,7 +666,7 @@ ozi_parse_routepoint(int field, char* str, waypoint* wpt_tmp)
     break;
   case 4:
     /* waypoint name */
-    wpt_tmp->shortname = csv_stringclean(str, ",");
+    wpt_tmp->shortname = csv_stringclean(str, QString(","));
     break;
   case 5:
     /* latitude */
@@ -697,7 +697,7 @@ ozi_parse_routepoint(int field, char* str, waypoint* wpt_tmp)
     break;
   case 13:
     /* description */
-    wpt_tmp->description = csv_stringclean(str, ",");
+    wpt_tmp->description = csv_stringclean(str, QString(","));
     break;
   default:
     break;
@@ -705,7 +705,7 @@ ozi_parse_routepoint(int field, char* str, waypoint* wpt_tmp)
 }
 
 static void
-ozi_parse_routeheader(int field, char* str, waypoint* wpt_tmp)
+ozi_parse_routeheader(int field, const QString& str, waypoint* wpt_tmp)
 {
 
   switch (field) {
@@ -716,7 +716,7 @@ ozi_parse_routeheader(int field, char* str, waypoint* wpt_tmp)
     break;
   case 1:
     /* route # */
-    rte_head->rte_num = atoi(str);
+    rte_head->rte_num = str.toInt();
     break;
   case 2:
     /* route name */
@@ -811,7 +811,7 @@ data_read(void)
           break;
         case rtedata:
           if (buff[0] == 'R') {
-            ozi_parse_routeheader(i, s, wpt_tmp);
+            ozi_parse_routeheader(i, QString(s), wpt_tmp);
             header = true;
           } else {
             ozi_parse_routepoint(i, s, wpt_tmp);
index 4130a67a30475a63d905064a431348fb26e3460d..1e5033076fba4bfef0bbbe0e84cd9d7d6a6e95f2 100644 (file)
@@ -70,7 +70,6 @@ data_read(void)
 {
   char* buff;
   char* s;
-  char* holder;
   waypoint* wpt_tmp;
   int i;
   int linecount = 0;
@@ -100,18 +99,13 @@ data_read(void)
           /* ignore: group  */
           break;
         case 1:
-          wpt_tmp->shortname = csv_stringtrim(s, "", 0);
+          wpt_tmp->shortname = csv_stringtrim(s, "");
           break;
         case 2:
           /* Description is not a TopoMapPro format requirement.
              If we assign "" then .loc/.gpx will generate empty XML tags :(
           */
-          holder = csv_stringtrim(s, "", 0);
-          if (strlen(holder)) {
-            wpt_tmp->description = holder;
-          } else {
-            xfree(holder);
-          }
+          wpt_tmp->description = csv_stringtrim(s, "");
           break;
         case 3:
           wpt_tmp->latitude = atof(s);
@@ -141,11 +135,12 @@ data_read(void)
              use the TopoMapLinks links.
              (plus discards length 0 strings (so no empty XML tags))
           */
-          holder = csv_stringtrim(s, "", 0);
-          if (strstr(holder, "http:") != NULL) {
-            wpt_tmp->AddUrlLink(holder);
+          {
+          QString link = csv_stringtrim(s, "");
+          if (link.contains("http:")) {
+            wpt_tmp->AddUrlLink(link);
+          }
           }
-          xfree(holder);
           break;
         default:
           /* whoa! nelly */